/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui;

import java.util.ArrayList;
import utsc.apps.utschedule.classes.Course;
import java.util.List;

import utsc.apps.utschedule.classes.GenericTask;
import utsc.apps.utschedule.classes.Timetable;
import utsc.apps.utschedule.ui.activities.CourseArchiveActivity;
import utsc.apps.utschedule.ui.activities.adapters.TimetableAdapter;
import utsc.apps.utschedule.util.database.DatabaseManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListFragment;

/**
 * A list fragment that displays the list of {@link Timetable archive timetables}. 
 *
 */
public class TimetableListFragment extends SherlockListFragment {
	
	private TimetableAdapter _timetableAdapter;
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getListView().setCacheColorHint(Color.TRANSPARENT);
		getView().setBackgroundColor(Color.LTGRAY);
		setEmptyText("No archive timetables found!");
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		if (_timetableAdapter==null) {
			//retrieve all the archive timetables from the database
			List<Timetable> archiveTimetables=DatabaseManager.getInstance(getSherlockActivity()).getArchiveTimetables();
			if (archiveTimetables!=null && !archiveTimetables.isEmpty()) {
				//try to calculate the sessional gpa of each timetable(term/session)
				DatabaseManager dbManager=DatabaseManager.getInstance(getSherlockActivity());
				for (Timetable timetable : archiveTimetables) {
					List<Course> courses=dbManager.getCoursesWithoutTutorials(timetable.get_timetableID());
					for (Course course : courses) {
						course.set_taskList(new ArrayList<GenericTask>(dbManager.getCourseTasks(course.get_courseID())));
					}
					timetable.set_gpaCalculator(courses);
				}
			}else {
				archiveTimetables=new ArrayList<Timetable>();
			}
			_timetableAdapter=new TimetableAdapter(getSherlockActivity(), android.R.layout.simple_list_item_1, archiveTimetables);
			setListAdapter(_timetableAdapter);
		}
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Timetable timetable= _timetableAdapter.getItem(position);
		Intent requestIntent=new Intent(getSherlockActivity(), CourseArchiveActivity.class);
		requestIntent.putExtra("TimetableID", timetable.get_timetableID());
		startActivity(requestIntent);
	}
}
